package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet(name = "Nachrichtenschleife", urlPatterns = { "/N1", "/N325" }, initParams = {
		@WebInitParam(name = "nachricht", value = "---"),
		@WebInitParam(name = "anzahl", value = "30") })
public class Nachrichtenschleife extends HttpServlet {

	private String nachricht;
	private String defaultNachricht = "Keine Nachricht erhalten.";
	private int durchlauf = 1;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		nachricht = config.getInitParameter("nachricht");
		if (nachricht == null)
			nachricht = defaultNachricht;
		try {
			String anzahlString = config.getInitParameter("anzahl");
			durchlauf = Integer.parseInt(anzahlString);
		} catch (NumberFormatException nfe) {
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.println(ServletUtilities.headWithTitle("Parameteruebergabe")
				+ "<body><h1>Uebergabeparameter</h1>");

		for (int i = 0; i < durchlauf; i++)
			out.println(nachricht + "<br />");
		out.println("</body></html>");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
